package com.momochen.alarmclockdemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.momochen.alarmclockdemo.R;
import com.momochen.alarmclockdemo.adapter.TimeAdapter;
import com.momochen.alarmclockdemo.db.AlarmBeanDaoHelper;
import com.momochen.alarmclockdemo.inter.OnClickRecyclerItemListener;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, OnClickRecyclerItemListener {

    TimeAdapter timeAdapter;

    RecyclerView rvContent;
    TextView tvAdd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();
    }

    private void initViews() {
        rvContent = (RecyclerView) this.findViewById(R.id.rv_content);
        tvAdd = (TextView) this.findViewById(R.id.tv_add);

        rvContent.setLayoutManager(new LinearLayoutManager(this));
        timeAdapter = new TimeAdapter();
        rvContent.setAdapter(timeAdapter);
        timeAdapter.addClearAndDatas(AlarmBeanDaoHelper.queryAll());

        tvAdd.setOnClickListener(this);

        timeAdapter.setOnClickRecyclerItemListener(this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_add:
                startActivityForResult(new Intent(this, EditActivity.class), 0);
//                AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
//
//                Intent intent = new Intent(ALARM_ALERT_ACTION);
//                Bundle bundle = new Bundle();
//                bundle.putSerializable(ALARM_RAW_DATA, new AlarmBean());
//                intent.putExtra("BUNDLE",bundle);
////                intent.putExtra(ALARM_RAW_DATA, new AlarmBean());
//
//                PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
//
//                am.set(AlarmManager.RTC_WAKEUP, SystemClock.currentThreadTimeMillis() + 1000, sender);
//                AlarmControl.enableAlert(this, new AlarmBean(), 1499654760000l);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0) {
            timeAdapter.addClearAndDatas(AlarmBeanDaoHelper.queryAll());
        }
    }

    @Override
    public void onClick(View view, int position) {
        Intent intent = new Intent(this, EditActivity.class);
        intent.putExtra("ALARM_BEAN", timeAdapter.getItem(position));
        startActivityForResult(intent, 0);
    }
}
